package com.woniu.service.Impl;

import com.woniu.dao.AnswerSheetDao;
import com.woniu.dao.ReplySheetDao;
import com.woniu.service.MarkPaperService;
import com.woniu.service.SearchPaperService;
import com.woniuxy.commons.entity.*;
import com.woniuxy.commons.entity.xql.MarkPapers;
import com.woniuxy.commons.enums.TestBankEnum;
import com.woniuxy.commons.service.RemoteAnswerService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: bzeducation
 * @author: Xingqilin
 * @create: 2020-07-28 10:22
 * @description:
 */

@Service
public class MarkPaperServiceImpl implements MarkPaperService {
    @Resource
    AnswerSheetDao answerSheetDao;
    @Resource
    ReplySheetDao replySheetDao;
    @Resource
    SearchPaperService searchPaperService;
    @Resource
    RemoteAnswerService remoteAnswerService;

    @Override
    public List<MarkPapers> getPapersName(long paperSubject, long paperType, String answerClass, long answerDeal) {
        List<MarkPapers> paperNames = answerSheetDao.getPaperNames(paperSubject, paperType, answerClass, answerDeal);
        paperNames.forEach(System.out::println);
        return paperNames;
    }

    @Override
    public List<AnswerSheet> getAnswerSheet(Integer paperId, Integer status) {
        List<AnswerSheet> answerSheetByStatusAndPaper = remoteAnswerService.getAnswerSheetByStatusAndPaper(paperId, status);
        return answerSheetByStatusAndPaper;
    }

    @Override
    public AnswerSheet getDetailAnswerSheet(Integer paperId, Integer userId, ReplySheet backSheet) {
        AnswerSheet answerSheetByRemoteUser = remoteAnswerService.getAnswerSheetByRemoteUser(paperId, userId);
//        TestPaper paperById = searchPaperService.getPaperById(answerSheetByRemoteUser.getTestPaper().getPaperId());
        searchPaperService.assemblePaper(answerSheetByRemoteUser.getTestPaper());
        double point = setSelectPoint(answerSheetByRemoteUser);
//        机器阅题数据存放
        backSheet.setReplyQuestionId(0);
        backSheet.setReplyScore(point);

        return answerSheetByRemoteUser;
    }

    @Override
    @Transactional
    public double setSelectPoint(AnswerSheet answerSheet) {
//        统计电脑阅卷总得分
        double computerPoint = 0;
//        获取试卷问题
        List<TestQuestion> paperQuestions = answerSheet.getTestPaper().getPaperQuestions();
//        获取学生答案
        List<ReplySheet> sheetAnswers = answerSheet.getSheetAnswers();
        int answerOffset = 0;
//        遍历试卷问题对比学生答案
        for (int i = 0; i < paperQuestions.size(); i++) {
            TestQuestion testQuestion = paperQuestions.get(i);
//            判断问题类型
            if (testQuestion.getPaperQuestionBank() == TestBankEnum.SELECT_BANK.getIndex()) {
//                转换成对应对象
                SelectBank selectBank = (SelectBank) testQuestion.getPaperDetailQuestion();
//                调用判分方法
                computerPoint += selectBankPoint(selectBank, sheetAnswers.get(i));
            } else if (testQuestion.getPaperQuestionBank() == TestBankEnum.MATERIAL_BANK.getIndex()) {
                MaterialBank materialBank = (MaterialBank) testQuestion.getPaperDetailQuestion();
                List<SelectBank> selectBanks = materialBank.getSelectBanks();
                List<JudgeBank> judgeBanks = materialBank.getJudgeBanks();

                for (int selectIndex = 0; selectIndex < selectBanks.size(); selectIndex++) {
                    computerPoint += selectBankPoint(selectBanks.get(selectIndex), sheetAnswers.get(i + answerOffset));
                    answerOffset++;
                }
                for (int judgeIndex = 0; judgeIndex < judgeBanks.size(); judgeIndex++) {
                    computerPoint += judgeBankPoint(judgeBanks.get(judgeIndex), sheetAnswers.get(i + answerOffset));
                    answerOffset++;
                }
            } else if (testQuestion.getPaperQuestionBank() == TestBankEnum.JUDGE_BANK.getIndex()) {
                JudgeBank judgeBank = (JudgeBank) testQuestion.getPaperDetailQuestion();
                computerPoint += judgeBankPoint(judgeBank, sheetAnswers.get(i));
            } else if (testQuestion.getPaperQuestionBank() == TestBankEnum.CLOZE_BANK.getIndex()) {
                ClozeBank clozeBank = (ClozeBank) testQuestion.getPaperDetailQuestion();
                List<ClozeSelect> clozeSelects = clozeBank.getClozeSelects();
                for (int clozeIndex = 0; clozeIndex < clozeSelects.size(); clozeIndex++) {
                    sheetAnswers.get(i + clozeIndex).setReplyScore(0);
                    if (clozeSelects.get(clozeIndex).getClozeSelectAnswer().equals(sheetAnswers.get(i + answerOffset).getReplyAnswer())) {
                        sheetAnswers.get(i + answerOffset).setReplyScore(clozeSelects.get(clozeIndex).getClozeSelectPoint());
                        sheetAnswers.get(i + answerOffset).setReplyPass(true);
                        computerPoint += clozeSelects.get(clozeIndex).getClozeSelectPoint();
                    }
                    replySheetDao.updateReplySheetPoint(sheetAnswers.get(i + answerOffset));
                    answerOffset++;
                }
            }
        }

        return computerPoint;
    }

    //    选择题判分
    private double selectBankPoint(SelectBank selectBank, ReplySheet replySheet) {
//        先将得分至0
        replySheet.setReplyScore(0);
//        判断答案正确性
        if (selectBank.getSelectAnswer().equals(replySheet.getReplyAnswer())) {
//            正确设置得分
            replySheet.setReplyScore(selectBank.getSelectPoint());
            replySheet.setReplyPass(true);
        }
        replySheetDao.updateReplySheetPoint(replySheet);
        return replySheet.getReplyScore();
    }

    private double judgeBankPoint(JudgeBank judgeBank, ReplySheet replySheet) {
        replySheet.setReplyScore(0);
        if (judgeBank.getJudgeAnswer().equals(replySheet.getReplyAnswer())) {
            replySheet.setReplyScore(judgeBank.getJudgePoint());
            replySheet.setReplyPass(true);
        }
        replySheetDao.updateReplySheetPoint(replySheet);
        return replySheet.getReplyScore();
    }

    @Override
    public String oneTopicPoint(ReplySheet replySheet) {
        int i = replySheetDao.updateReplySheetPoint(replySheet);
        return i + "";
    }

    @Override
    public String finishMark(Long sheetId, double sheetScore) {

        return null;
    }

}
